package event

import (
	"fmt"
	"gitchina/CPTT4WeChat/errors"
	"gitchina/CPTT4WeChat/models/handle"
	"gitchina/CPTT4WeChat/util"
	"strconv"
)

var funcMap = make(map[string]func(*WeChatRequestMessage) string)

type ClickEvent struct {
	*PushEvent
	EventKey string
}

func init() {
	funcMap["11001"] = func(rq *WeChatRequestMessage) string { return onEvtKey11001Called(rq) }
	funcMap["11002"] = func(rq *WeChatRequestMessage) string { return onEvtKey11002Called(rq) }
	funcMap["11003"] = func(rq *WeChatRequestMessage) string { return onEvtKey11003Called(rq) }
	funcMap["11004"] = func(rq *WeChatRequestMessage) string { return onEvtKey11004Called(rq) }
	funcMap["11005"] = func(rq *WeChatRequestMessage) string { return onEvtKey11005Called(rq) }
	funcMap["11006"] = func(rq *WeChatRequestMessage) string { return onEvtKey11006Called(rq) }
}

func (e *ClickEvent) Handle() interface{} {
	var rsMsg string
	if fn, ok := funcMap[e.EventKey]; ok {
		rsMsg = fn(e.WeChatRequestMessage)
	} else {
		rsMsg = errors.Invalid_Event
	}
	return CreateTextResponseMessage(e.WeChatRequestMessage, rsMsg)
}

func (e *ClickEvent) OnParse(params map[string]string) Event {
	e.EventKey = params["eventkey"]
	return e
}

func onEvtKey11001Called(rq *WeChatRequestMessage) string {
	if cache, ok := util.GetCache(util.User_Channel); ok {
		cache.Delete(rq.FromUserName)
	}
	var output string
	stats := &handle.Stats{}
	if list, err := stats.GetCheckInStatListGroupByGrade(rq.FromUserName); err == nil {
		output = list.Output("晨检年级统计")
	} else {
		output = err.Error()
	}
	return output
}

func onEvtKey11002Called(rq *WeChatRequestMessage) string {
	var output string
	var u handle.User
	stats := &handle.Stats{}
	if usrInfo, ok := u.GetByOpenId(rq.FromUserName); ok {
		if usrInfo.GetUserType() == 1 {
			if cache, ok := util.GetCache(util.User_Channel); ok {
				cache.Set(rq.FromUserName, getCheckInClassOptionsFn(rq, stats))
			}
			if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
				output = opts.Output("")
			} else {
				output = err.Error()
			}
		} else {
			if list, err := stats.GetCheckInStatListGroupByClass(rq.FromUserName, 0); err == nil {
				output = list.Output("晨检班级统计")
			} else {
				output = err.Error()
			}
		}
	} else {
		output = errors.User_Not_Bind
	}

	return output
}

func onEvtKey11003Called(rq *WeChatRequestMessage) string {
	stats := &handle.Stats{}
	title := "您可以输入[学号]或[姓名]查询学生出勤详情:"
	if cache, ok := util.GetCache(util.User_Channel); ok {
		var rsMsg string
		cache.Set(rq.FromUserName, func(input string) string {
			if list, err := stats.GetCheckInOutList(rq.FromUserName, input, 1); err == nil {
				rsMsg = list.Output("晨检个人详情")
				rsMsg += fmt.Sprintf("\n%s", title)
			} else {
				rsMsg = err.Error()
			}

			return rsMsg
		})
	}
	return title
}

func onEvtKey11004Called(rq *WeChatRequestMessage) string {
	if cache, ok := util.GetCache(util.User_Channel); ok {
		cache.Delete(rq.FromUserName)
	}
	var output string
	stats := &handle.Stats{}
	if list, err := stats.GetCheckOutStatListGroupByGrade(rq.FromUserName); err == nil {
		output = list.Output("晚接年级统计")
	} else {
		output = err.Error()
	}
	return output
}

func onEvtKey11005Called(rq *WeChatRequestMessage) string {
	var output string
	var u handle.User
	stats := &handle.Stats{}
	if usrInfo, ok := u.GetByOpenId(rq.FromUserName); ok {
		if usrInfo.GetUserType() == 1 {
			if cache, ok := util.GetCache(util.User_Channel); ok {
				cache.Set(rq.FromUserName, getCheckOutClassOptionsFn(rq, stats))
			}
			if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
				output = opts.Output("")
			} else {
				output = err.Error()
			}
		} else {
			if list, err := stats.GetCheckInStatListGroupByClass(rq.FromUserName, 0); err == nil {
				output = list.Output("晚接班级统计")
			} else {
				output = err.Error()
			}
		}
	} else {
		output = errors.User_Not_Bind
	}

	return output

	// var output string
	// stats := &handle.Stats{}
	// if cache, ok := util.GetCache(util.User_Channel); ok {
	// 	var rsMsg string
	// 	cache.Set(rq.FromUserName, func(input string) string {
	// 		if opt, err := strconv.Atoi(input); err == nil {
	// 			if list, err := stats.GetCheckOutStatListGroupByClass(rq.FromUserName, opt); err == nil {
	// 				rsMsg += list.Output("晚接班级统计")
	// 				if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
	// 					rsMsg += fmt.Sprintf("\n%s", opts.Output(""))
	// 				}
	// 			} else {
	// 				rsMsg = err.Error()
	// 			}

	// 		} else {
	// 			rsMsg = errors.Invalid_Options_Input
	// 			util.GetLogger().Error("[event-click] invalid input:%s", input)
	// 		}
	// 		return rsMsg
	// 	})
	// }

	// if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
	// 	output = opts.Output("")
	// } else {
	// 	output = err.Error()
	// }
	// return output
}

func onEvtKey11006Called(rq *WeChatRequestMessage) string {
	stats := &handle.Stats{}
	title := "您可以输入[学号]或[姓名]查询学生晚接详情:"
	if cache, ok := util.GetCache(util.User_Channel); ok {
		var rsMsg string
		cache.Set(rq.FromUserName, func(input string) string {
			if list, err := stats.GetCheckInOutList(rq.FromUserName, input, 2); err == nil {
				rsMsg = list.Output("晚接个人详情")
				rsMsg += fmt.Sprintf("\n%s", title)
			} else {
				rsMsg = err.Error()
			}

			return rsMsg
		})
	}
	return title
}

func getCheckInClassOptionsFn(rq *WeChatRequestMessage, stats *handle.Stats) func(input string) string {
	fn := func(input string) string {
		var rsMsg string
		if opt, err := strconv.Atoi(input); err == nil {
			if list, err := stats.GetCheckInStatListGroupByClass(rq.FromUserName, opt); err == nil {
				rsMsg += list.Output("晨检班级统计")
				if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
					rsMsg += fmt.Sprintf("\n%s", opts.Output(""))
				}
			} else {
				rsMsg = err.Error()
			}

		} else {
			rsMsg = errors.Invalid_Options_Input
			util.GetLogger().Error("[event-click] invalid input:%s", input)
		}
		return rsMsg
	}
	return fn
}

func getCheckOutClassOptionsFn(rq *WeChatRequestMessage, stats *handle.Stats) func(input string) string {
	fn := func(input string) string {
		var rsMsg string
		if opt, err := strconv.Atoi(input); err == nil {
			if list, err := stats.GetCheckOutStatListGroupByClass(rq.FromUserName, opt); err == nil {
				rsMsg += list.Output("晚接班级统计")
				if opts, err := stats.GetCheckInOutOptionsGroupByGrade(rq.FromUserName); err == nil {
					rsMsg += fmt.Sprintf("\n%s", opts.Output(""))
				}
			} else {
				rsMsg = err.Error()
			}

		} else {
			rsMsg = errors.Invalid_Options_Input
			util.GetLogger().Error("[event-click] invalid input:%s", input)
		}
		return rsMsg
	}
	return fn
}
